package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysOperationLogService;
import org.asiainfo.system.domain.vo.SysOperationLogVo;
import org.asiainfo.system.domain.bo.SysOperationLogBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operationLog")
public class SysOperationLogController extends BaseController {

    private final SysOperationLogService sysOperationLogService;

    /**
     * 分页查询
     *
     * @param sysOperationLogBo 筛选条件
     * @param pageQuery         分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:operationLog:list")
    @GetMapping("/list")
    public TableDataInfo<SysOperationLogVo> list(SysOperationLogBo sysOperationLogBo, PageQuery pageQuery) {
        return sysOperationLogService.queryPageList(sysOperationLogBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param operationId 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:operationLog:query")
    @GetMapping("/{operationId}")
    public R<SysOperationLogVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("operationId") Long operationId) {
        return R.ok(sysOperationLogService.queryById(operationId));
    }

    /**
     * 删除数据
     *
     * @param operationIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:operationLog:remove")
    @Log(title = "操作日志记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] operationIds) {
        return toAjax(sysOperationLogService.deleteWithValidByIds(List.of(operationIds)));
    }

    /**
     * 清空数据
     *
     * @return 删除是否成功
     */
    @SaCheckPermission("system:operationLog:remove")
    @Log(title = "操作日志记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/clear")
    public R<Void> clear() {
        return toAjax(sysOperationLogService.deleteAll());
    }
}

